package com.topboya.linker.diyview;

import android.app.Service;
import android.content.Context;
import android.os.Vibrator;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.haibin.calendarview.CalendarLayout;
import com.topboya.linker.utils.LogUtils;

public class MyRelativeLayout extends RelativeLayout {

        private static final String TAG = "MyRelativeLayout:";
        private Context mContext;
        private RelativeLayout rel_top;
        private RecyclerView rl_depart_member;
        private CalendarLayout calendarLayout;
        float scale;
        float firstY, lastY;
        int height;
        boolean isPullDown = false;//是否是向下滑动 true 是
        boolean isAnimation = false;//动画是否已开启过 true 是
        final int HEIGHT=180;//菜单布局正常高度
        final int HEIGHT_DOT=120;//三个小球布局正常高度

    public MyRelativeLayout(Context context) {
            super(context);

        }

    public MyRelativeLayout(Context context, AttributeSet attrs) {
            super(context, attrs);

        }

    public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);

        }
        public void setChild(Context context, RelativeLayout rel_top , RecyclerView rl_depart_member, CalendarLayout calendarLayout){
            this.mContext = context;
            this.rel_top = rel_top;
            this.rl_depart_member = rl_depart_member;
            this.calendarLayout =calendarLayout;


        }
        @Override
        protected void onFinishInflate() {
            super.onFinishInflate();
            scale = getResources().getDisplayMetrics().density;
        }

        @Override
        public boolean dispatchTouchEvent(MotionEvent event) {
//
//            if(calendarLayout.isExpand()) {
//                LogUtils.v(TAG +"dispatchTouchEvent");
//
//                switch (event.getAction()) {
//
//                    case MotionEvent.ACTION_DOWN:
//                        firstY = event.getY();
////                        if (firstY < 280){
////                            return super.dispatchTouchEvent(event);
////                        }
//                        break;
//                    case MotionEvent.ACTION_MOVE:
//                        LogUtils.v(TAG + "ACTION_MOVE");
//                        lastY = event.getY();
//                        height = (int) ((Math.abs(lastY - firstY) / 5) * scale + 0.5f);//根据不同手机分辨率设置高度
//                        if (lastY - firstY > 0 && (Math.abs(lastY - firstY) > 25) && rel_top.getHeight() != HEIGHT) {//向下滑动 (ScrollView滑动在顶部)
//                             LogUtils.v(TAG + "ACTION_MOVE1");
//                            isPullDown = true;
//                            setHeight(height);
//                            if (height >= HEIGHT_DOT) {
//                                if (height >= 125 && height < 135) {
//                                    //震动（感觉没管用，后面在看看）
//                                    Vibrator vib = (Vibrator)mContext.getSystemService(Service.VIBRATOR_SERVICE);
//                                    vib.vibrate(60);
//
//                                }
//                            }
//                            return true;
//                        } else if (lastY - firstY < 0 && rel_top.getHeight() > 0 && height <= HEIGHT) {//向上滑动
//                            LogUtils.v(TAG + "ACTION_MOVE2---height:"+height);
//                            isPullDown = false;
//                            setHeight(HEIGHT - height);
//                            return true;
//                        }
//                        break;
//                    case MotionEvent.ACTION_UP:
//                        isAnimation = false;//状态重置
//                        if (isPullDown) {
//                            if (height < HEIGHT) {//向下滑动距离较小，直接消失
//                                isPullDown = false;
//                                setHeight(0);
//                            } else if (height >= HEIGHT) {//向下滑动距离较大，直接固定高度
//                                setHeight(HEIGHT);
//                            }
//                        } else if (rel_top.getHeight() > 0) {//向上滑动，并且菜单已调出，直接隐藏菜单
//                            setHeight(0);
//                        }
//                        break;
//                }
//            }
            return super.dispatchTouchEvent(event);
        }


        /**
         * 位移动画
         * @param fromX 初始X位置
         * @param toX   结束X位置
         * @param fromY 初始Y位置
         * @param toY   结束Y位置
         * @param time  动画时间
         * @param view  控件
         */
        private void setAnimation(float fromX,float toX,float fromY,float toY,long time,View view){
            TranslateAnimation translate = new TranslateAnimation(
                    Animation.RELATIVE_TO_SELF, fromX,
                    Animation.RELATIVE_TO_SELF, toX,
                    Animation.RELATIVE_TO_SELF, fromY,
                    Animation.RELATIVE_TO_SELF, toY);
            translate.setDuration(time);
            translate.setFillAfter(true);//true 设置动画结束后，控件位置保持不动，false 则是返回初始位置
            view.startAnimation(translate);
        }

        /**
         * 设置高度
         * @param height
         */
        private void setHeight(int height) {
            RelativeLayout.LayoutParams lineParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
            rel_top.setLayoutParams(lineParams);

        }
}
